package com.popo.xml;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;

public class Dom4jTest {
	//存储xml元素信息的容器    
	 private static ArrayList<Leaf> elemList = new ArrayList<Leaf>();   

	public static String systemId = "<project default=\"usage\" basedir=\".\">"
			+ "	<path id=\"compile.classpath\">"
			+ "   	<fileset dir=\"./lib/endorsed\">  "
			+ "	    	<include name=\"*.jar\" />"
			+ "  		</fileset>"
			+ "   	<fileset dir=\"./lib\">   "
			+ "      		<include name=\"*.jar\" >"
			+ "book"
			+ "</include>"
			+ "   	</fileset> "
			+ "	</path>"
			+ "	<path id=\"endorsed.classpath\">"
			+ "        <fileset dir=\"./lib/endorsed\">  "
			+ "	   <include name=\"*.jar\" />"
			+ "        </fileset>    </path>   <path id=\"test.classpath\">"
			+ "        <path refid=\"compile.classpath\" />        <fileset dir=\"./lib/test\">"
			+ "            <include name=\"**/*.jar\" />        </fileset>"
			+ "        <fileset dir=\"./lib/tools\">            <include name=\"xalan*.jar\" />    "
			+ "        <include name=\"xerces*.jar\" />        </fileset>    </path>   "
			+ " <path id=\"samples.classpath\">        <fileset dir=\"./lib/tools\">  "
			+ "          <include name=\"**/*.jar\" />        </fileset>"
			+ "        <path refid=\"test.classpath\" />        <pathelement location=\".\"/>    </path>"
			+ "</project>";

	public static void main(String[] args) {
		SAXReader saxReader = new SAXReader();

		try {
			Document document = DocumentHelper.parseText(systemId);

			Element element = document.getRootElement();
			System.out.println(element.getName());
			List<Element> elements = element.elements();

			printElement(element);

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	static int i = 0;

	private static void printElement(Element element) {
		// TODO Auto-generated method stub
		List elements = element.elements();
		// 没有子元素
		if (elements.isEmpty()) {
			String xpath = element.getPath();
			String value = element.getTextTrim();
			System.out.println("xpath="+xpath+",value="+value);
		} else {
			// 有子元素
			Iterator it = elements.iterator();
			while (it.hasNext()) {
				Element elem = (Element) it.next();
				// 递归遍历
				printElement(elem);
			}
		}
	}

	/**
	 * 获取节点所有属性值 <功能详细描述>
	 * 
	 * @param element
	 * @return [类、类#方法、类#成员]
	 */
	public String getNoteAttribute(Element element) {
		String xattribute = "";
		DefaultAttribute e = null;
		List list = element.attributes();
		for (int i = 0; i < list.size(); i++) {
			e = (DefaultAttribute) list.get(i);
			// System.out.println("name = " + e.getName() + ", value = " +
			// e.getText());
			xattribute += " [name = " + e.getName() + ", value = "
					+ e.getText() + "]";
		}
		return xattribute;
	}

	/**
	 * 递归遍历方法 63. * <功能详细描述> 64. * @param element 65. * @see [类、类#方法、类#成员] 66.
	 */
	public void getElementList(Element element) {
		List elements = element.elements();
		// 没有子元素
		if (elements.isEmpty()) {
			String xpath = element.getPath();
			String value = element.getTextTrim();
			elemList.add(new Leaf(getNoteAttribute(element), xpath, value));
		} else {
			// 有子元素
			Iterator it = elements.iterator();
			while (it.hasNext()) {
				Element elem = (Element) it.next();
				// 递归遍历
				getElementList(elem);
			}
		}
	}

	public String getListString(List elemList) {
		StringBuffer sb = new StringBuffer();
		for (Iterator it = elemList.iterator(); it.hasNext();) {
			Leaf leaf = (Leaf) it.next();
			sb.append("xpath: " + leaf.getXpath()).append(", value: ")
					.append(leaf.getValue());
			if (!"".equals(leaf.getXattribute())) {
				sb.append(", Attribute: ").append(leaf.getXattribute());
			}
			sb.append("\n");
		}
		return sb.toString();
	}
}

class Leaf {
	// 节点属性
	private String xattribute;

	// 节点PATH
	private String xpath;

	// 节点值
	private String value;

	public Leaf(String xattribute, String xpath, String value) {
		this.xattribute = xattribute;
		this.xpath = xpath;
		this.value = value;
	}

	public String getXpath() {
		return xpath;
	}

	public void setXpath(String xpath) {
		this.xpath = xpath;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getXattribute() {
		return xattribute;
	}

	public void setXattribute(String xattribute) {
		this.xattribute = xattribute;
	}
}
